<template>
  <div class="employees">
    <el-card class="top-card">
      <div class="btn">
        <el-button
          v-buttonShow:inlineBlock="'employees_importBtn'"
          type="primary"
          @click="importClick"
        >导入</el-button>
        <el-button
          v-buttonShow:inlineBlock="'employees_outputBtn'"
          type="primary"
          @click="outPutClick"
        >导出</el-button>
        <el-button type="primary" @click="addEvent">+ 新增员工</el-button>
      </div>
    </el-card>
    <el-card>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" sortable />
        <el-table-column label="相片" prop="staffPhoto" sortable>
          <template v-slot="{ row }">
            <img
              v-globalImgerror="defaultImg"
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              @click="btnClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" sortable />
        <el-table-column label="工号" prop="workNumber" sortable />
        <!-- <el-table-column
          label="聘用形式"
          :formatter="formatter"
          prop="formOfEmployment"
          sortable
        /> -->
        <el-table-column label="聘用形式" prop="formOfEmployment" sortable />
        <el-table-column label="部门" prop="departmentName" sortable />
        <el-table-column label="入职时间" prop="timeOfEntry" sortable />
        <el-table-column label="状态" prop="enableState" sortable />
        <el-table-column label="操作" sortable width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          layout="total,sizes,prev,pager,next,jumper"
          :total="page.total"
          :page-sizes="[1, 5, 10, 100, 200, 300]"
          :page-size="page.size"
          :current-page="page.page"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <Add ref="add" @getData="getData" />
    <el-dialog title="二维码" width="500px" :visible.sync="show">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <Role ref="role" :is-show.sync="isShow" />
  </div>
</template>

<script>
import Qrcode from 'qrcode'
import Add from './components/add.vue'
import employeesData from '@/api/constant/employees'
import { sysUser, sysUserDelete } from '@/api/employees'
import moment from 'moment'
import cookieJs from 'js-cookie'
import Role from './components/role.vue'
export default {
  components: {
    Add,
    Role
  },
  data() {
    return {
      isShow: false,
      show: false,
      defaultImg: require('@/assets/common/header.jpg'),
      list: [],
      employeesData,
      page: {
        total: 100,
        size: +cookieJs.get('size') || 5,
        page: 1
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.page)
      res.data.rows.forEach((item) => {
        const result = employeesData.hireType.find((item2) => {
          return item2.id === +item.formOfEmployment
        })
        const resultStr = result ? result.value : '-'
        item.formOfEmployment = resultStr
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })
      this.list = res.data.rows
      // console.log(this.list)
      this.page.total = res.data.total
    },
    // 改变页容量
    sizeChange(size) {
      this.page.size = size
      cookieJs.set('size', size)
      this.page.page = 1
      this.getData()
    },
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    // formatter(row, column, cellValue, index) {
    //   const result = employeesData.hireType.find((item) => {
    //     return item.id === cellValue
    //   })
    //   return result ? result.value : '-'
    // }
    addEvent() {
      this.$refs.add.isShow = true
    },
    del(id) {
      this.$confirm('您确定要删除该员工吗?', '提示')
        .then(async() => {
          await sysUserDelete(id)
          this.$message.success('删除成功')
          this.pageChange(1)
        })
        .catch(() => {})
    },
    importClick() {
      this.$router.push('/employees/import')
    },
    goDetail(id) {
      this.$router.push('/employees/detail/' + id)
    },
    async outPutClick() {
      const header = {
        姓名: 'username', //  string	非必须		姓名
        手机号: 'mobile', //  string	非必须		手机号
        入职时间: 'timeOfEntry', //  string	非必须		入职时间
        聘用形式: 'formOfEmployment', // 	number	非必须		聘用形式
        工号: 'workNumber', //  string	非必须		工号
        组织名称: 'departmentName', //  string	非必须		组织名称
        转正时间: 'correctionTime' // string	非必须		转正时间
      }
      const res = await sysUser({
        page: 1,
        size: 10000
      })
      res.data.rows.forEach((item) => {
        const result = employeesData.hireType.find((item2) => {
          return item2.id === +item.formOfEmployment
        })
        const resultStr = result ? result.value : '-'
        item.formOfEmployment = resultStr
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
        item.correctionTime = moment(item.correctionTime).format('YYYY-MM-DD')
      })
      // 循环整个列表数据：位了产生一个只有值的二维数据[[]],它的数据次序要按照又不数组次序来
      const data = res.data.rows.map((item) => {
        // 循环头部数据
        return Object.keys(header).map((item2) => {
          // 按钮次序return相应项的值，最终产生按次序的新数组
          return item[header[item2]]
        })
      })
      console.log(data)
      import('@/vendor/Export2Excel')
        .then((res) => {
          res.export_json_to_excel({
            header: Object.keys(header),
            data,
            filename: '用户列表'
          })
        })
        .catch(() => {})
    },
    btnClick(Img) {
      this.show = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, Img, { width: 300, height: 300 })
      })
    },
    roleClick(id) {
      this.isShow = true
      this.$refs.role.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  .top-card {
    margin-bottom: 25px;
    .btn {
      text-align: right;
    }
  }
  .page {
    padding-top: 15px;
    text-align: right;
  }
  .avatar {
    width: 100%;
  }
}
</style>
